package gui;

import business.CalendarManager;
import business.UserManager;
import entity.Calendar;
import entity.CalendarScope;
import entity.Event;
import entity.Invitation;
import entity.Notification;
import entity.User;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import utils.MessageCode;

@Named(value = "userBean")
@RequestScoped
public class UserBean {
    
    @EJB
    private UserManager userManager;
    @EJB
    private CalendarManager calendarManager;
    
    private User currentUser;
    private String password;
    
    public UserBean() {
    }
    
    @PostConstruct
    public void loadUser() {
        currentUser = userManager.getLoggedUser();
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public String getPassword() {
        return password;
    }
    
    public String save() {
        userManager.save(currentUser);
        return "home?faces-redirect=true";
    }
    
    public String cancelProfile() {
        userManager.delete(currentUser);
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        try {
            request.logout();
            request.getSession().invalidate();
        } catch (ServletException e) {
            context.addMessage(null, new FacesMessage("Logout failed."));
        }
        return "/systemMessage?faces-redirect=true&msgCode=" + MessageCode.PROFILE_CANCELLED.toString();
    }
    
    public String toggleCalendarScope() {
        Calendar calendar = currentUser.getCalendars().get(0);
        if (calendar.isPublic()) {
            calendar.setScope(CalendarScope.PRIVATE.toString());
        }else if(calendar.isPrivate()) {
            calendar.setScope(CalendarScope.PUBLIC.toString());
        }
        calendarManager.save(calendar);
        return "home?faces-redirect=true";
    }
    
    public List<Event> getUpcomingEvents() {
        List<Event> upcomingEvents = new ArrayList<>();
        Date now = new Date();
        currentUser = userManager.refresh(currentUser);
        for (Event event : currentUser.getCalendars().get(0).getEvents()) {
            if (now.before(event.getDate())) {
                upcomingEvents.add(event);
            }
        }
        Collections.sort(upcomingEvents, (Event event1, Event event2) -> {
            return event1.getDate().compareTo(event2.getDate());
        });
        return upcomingEvents;
    }
    
    public List<Notification> getLoggedUserNotifications() {
        currentUser = userManager.refresh(currentUser);
        return currentUser.getNotifications();
    }
    
    public List<Invitation> getLoggedUserInvitations() {
        currentUser = userManager.refresh(currentUser);
        return currentUser.getReceivedInvitations();
    }
    
    public boolean isCreator(Event event) {
        return currentUser.getCreatedEvents().contains(event);
    }
    
    public boolean isCurrentCalendarPublic() {
        return currentUser.getCalendars().get(0).isPublic();
    }
    
    public boolean isCurrentCalendarPrivate() {
        return currentUser.getCalendars().get(0).isPrivate();
    }
}